//
// Created by Administrator on 2023/8/6.
//
#include <iostream>

using namespace std;


// Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode *next;

    ListNode() : val(0), next(nullptr) {}

    ListNode(int x) : val(x), next(nullptr) {}

    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode *swapPairs(ListNode *head) {
        // 交换链表中的相邻节点
        // 递归
        if (head == nullptr || head->next == nullptr) return head;
        ListNode *next = head->next;
        head->next = swapPairs(next->next);
        next->next = head;
        return next;
    }
};

int main() {
    ListNode n1(1), n2(2), n3(3), n4(4);
    // 1 的下一个是 3 4 的结果
    // 2 的下一个是1
    n1.next = &n2;
    n2.next = &n3;
    n3.next = &n4;
    Solution s;
    ListNode *head = s.swapPairs(&n1);
    while (head) {
        cout << head->val << endl;
        head = head->next;
    }
    return 0;
}